#!/usr/bin/python

from urllib import parse
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import time
import requests
import json
import urllib
import re

mobile = sys.argv[1]
webhook = "https://oapi.dingtalk.com/robot/send?access_token=bd38f7e0be7a15dd0db6d8d89a9bff3d722ad96c763a7492ce5a5c0822729744"

url = "http://47.99.139.72:8888/callback?hub=qingning-live-sec&streamId=z1.test.stream1&stream=123456&rate=0.9&time=123411111&ts=333333.ts&label=sexy&review=false"
params = parse.parse_qs(parse.urlparse( url ).query)

#判断类型
if re.findall(str(params['label']), "sexy"):
    params['label'] = "性感"
elif re.findall(str(params['label']), "porn"):
    params['label'] == "涉黄"
elif re.findall(str(params['label']), "normal"):
    params['label'] == "正常"
else:
    params['label'] == "未知"

#判断是否需要复审
if re.findall(str(params['review']), "false"):
    params['review'] = "不需要"
else:
    params['review'] = "需要"

#替换字典key
params.update(存储空间 = params.pop("hub"), 完整流名 = params.pop("streamId"), 房间号 = params.pop("stream"), 准确率 = params.pop("rate"), 人工复审 = params.pop("review"), 时间 = params.pop("time"),类型 = params.pop("label"), 文件 = params.pop("ts"),)

result = ""
for key, value in params.items():
    value = "".join(value)
    result += str(key) +": "+ str(value) + "\n"

def Robot_Send(message):
    url = webhook
    headers = {
        "Content-Type": "application/json ;charset=utf-8 "
    }
    data = {
        "msgtype": "text",
        "text": {"content": message},
        "at": {
            "atMobiles": [ mobile ],
            "isAtAll": False
        }
    }
    format_data = json.dumps(data, sort_keys=True,indent = 4) #转为json
    res = requests.post(url, data=format_data, headers=headers)
    return res

if __name__ == '__main__':
    Robot_Send(result)

